package com.example.controller;

import com.example.dao.DiscussDao;
import com.example.dao.IcommentDao;
import com.example.dao.LikeDiscussDao;
import com.example.dao.UserInfoDao;
import com.example.model.dto.DiscussionDTO;
import com.example.model.pojo.Comment;
import com.example.model.pojo.Discussion;
import com.example.model.pojo.Result;
import com.example.model.pojo.UserInfo;
import com.example.model.vo.CommentOneVo;
import com.example.service.DiscussService;
import com.example.untils.JJWTUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("discuss")
public class DiscussController {
    @Autowired
    private DiscussService discussService;
    @Autowired
    private DiscussDao discussDao;
    @Autowired
    private LikeDiscussDao likeDiscussDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    public IcommentDao icommentDao;
    @Autowired
    public UserInfoDao userInfoDao;

    @PostMapping("read")
    public Result updateStatus(Integer commentid)
    {
        icommentDao.updateStutus(commentid);
        return  new Result("200","success");
    }


    @GetMapping("getOne")
    public  Result getInfo(Integer id)
    {
        Discussion discussion= discussDao.findById(id).get();
        DiscussionDTO discussionDTO=new DiscussionDTO();
        discussionDTO.setDiscussion(discussion);
        Integer disscussid=discussion.getId();
        Integer likenum= likeDiscussDao.getDiscussCount(disscussid);
        discussionDTO.setCount(likenum);
        return  new Result("200","success",discussionDTO);
    }


    @GetMapping("next")
    public Result nextPage(Integer pageNum,Integer pageSize){
        Page<Discussion> page=discussService.loadAll(pageNum-1,pageSize);

        return  new Result("200","success",page);
    }
    @GetMapping("hot")
    public Result hotPage(Integer pageNum,Integer pageSize){
        Page<Discussion> page=discussService.loadHotDiscussService(pageNum-1,pageSize);
        return  new Result("200","success",page);
    }
    @GetMapping("new")
    public Result newPage(Integer pageNum,Integer pageSize){
        Page<Discussion> page=discussService.loadNewDiscussService(pageNum-1,pageSize);
        return  new Result("200","success",page);
    }
    @GetMapping("getSize")
    public Result getSize(){
        int dataSize=discussService.getDiscussSize();
        return new Result("200","success",dataSize);
    }

    @GetMapping("comment")
    public  Result getComment(Integer clientID)
    {
        System.out.println("客户端ID====>"+clientID);
        List<CommentOneVo> list=new ArrayList<>();
        List<Discussion > discussions=discussDao.findAllByUid(clientID);
        for (Discussion discussion : discussions) {
            Integer disId=discussion.getId();
            List<Comment> comments=icommentDao.findAllByDiscussId(disId);
            for (Comment comment : comments) {
                CommentOneVo commentOneVo=new CommentOneVo();
                UserInfo userInfo1=userInfoDao.getByUuid( comment.getUuId());
                commentOneVo.setContent(comment.getContent());
                commentOneVo.setDiscussId(comment.getDiscussId());
                commentOneVo.setCId(comment.getCId());
                if(null!=userInfo1)
                    commentOneVo.setCavatar(userInfo1.getAvatar());
                list.add(commentOneVo);
            }
        }
        System.out.println("===============");
        list.forEach(System.out::println);
        return  new Result("200","success",list);
    }


    @PostMapping("newdiscuss")
    public Result saveDiscuss(@RequestBody Map<String,Object> map, HttpServletRequest request){
        UserInfo userInfo= (UserInfo) request.getAttribute("userInfo");
        Discussion discussion=new Discussion();
        discussion.setContent(map.get("content").toString());
        System.out.println(userInfo);
        discussion.setAuthor(userInfo.getNickname());
        discussion.setAvatar(userInfo.getAvatar());
        discussion.setRole("user");
        discussion.setDescription(map.get("description").toString());
        discussion.setTitle(map.get("title").toString());
        discussion.setGmtCreate(new Timestamp(new Date().getTime()));
        discussion.setGmtModified(new Timestamp(new Date().getTime()));
        discussion.setUid(userInfo.getUuid());
        discussion.setLikeNum(0);
        discussDao.save(discussion);
        return  new Result("400","success",discussion);
    }

}
